package com.gameGroups.Controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gameGroups.Dto.GiveLikeDto;
import com.gameGroups.Dto.UserInfoDto;
import com.gameGroups.Dto.WorksDto;
import com.gameGroups.Dto.WorksExtDto;
import com.gameGroups.Service.GiveLikeNumberService;
import com.gameGroups.Service.UpdateUserInfoService;
import com.gameGroups.Service.WorksAndArticleService;
import com.gameGroups.Utils.MyException;
import com.gameGroups.Utils.StringUtil;


@Controller
@RequestMapping("/worksArticle")
public class WorksAndArticleController {
   @Autowired
   private WorksAndArticleService worksAndArticleService;
   @Autowired
   private UpdateUserInfoService updateUserInfoService;
   @Autowired
   private GiveLikeNumberService giveLikeNumberService;
   
   //首页查询作品
   @RequestMapping(value="/query.do",method=RequestMethod.POST)
   @ResponseBody
   public Map<String,Object> queryWorksAndArticle(HttpServletRequest request,HttpServletResponse response) throws MyException{  
	   Map<String,Object> model=new HashMap<String, Object>();    
	   Map<String,Object> param=new HashMap<String, Object>();
	   try {		   
		   //如果是1就是根据点赞来排,2是根据时间排序
		   int pageNumber =Integer.parseInt(request.getParameter("pageNumber").toString());
		   String homeTable = request.getParameter("homeTable");
		   if ("tab1".equals(homeTable)) {
		   String focus=request.getParameter("focus");
		   List<String> focusList =new ArrayList<String>();
		   if (focus!=null) {
			   String[] split = focus.split(",");
			   focusList =new ArrayList<String>();
			    for (int i = 0; i < split.length; i++) {
			    	focusList.add(split[i]);
				 }
	       }else{
	    	   focusList.add("A");
	       }
		   param.put("focus", focusList);
		   }else if("tab3".equals(homeTable)){
		   String city=request.getParameter("city");	
		   param.put("city", city);
		   }
		   pageNumber=(pageNumber-1)*10;
		   param.put("pageNumber", pageNumber);
		   param.put("homeTable", homeTable);

		   if (pageNumber==0) {
			   param.put("pageSize",5);
			   List<WorksDto> worksList= worksAndArticleService.queryWorksListByGiveLike(param);
		       //从第五行开始
			   param.put("pageNumber", 5);
			   List<WorksDto> worksListUpdate= worksAndArticleService.queryWorksListByUpdate(param);
			   worksList.addAll(worksListUpdate);
			 if (StringUtil.isNullOrBlank(worksList)) {
				  model.put("status", "5555");
				  model.put("errorMessage", "作品不存在！");
				 }else{
				  model.put("status", "0000"); 
				  List<WorksDto> worksUtil = worksUtil(worksList);
				  model.put("worksList", worksUtil);
				 }
			   return model;
		    }else{
		       param.put("pageSize",10);
		       List<WorksDto> worksList = worksAndArticleService.queryWorksListByUpdate(param);
		       if (StringUtil.isNullOrBlank(worksList)) {
				  model.put("status", "5555");
				  model.put("errorMessage", "作品不存在！");
			      }else{
				  model.put("status", "0000"); 
				  List<WorksDto> worksUtil = worksUtil(worksList);
				  model.put("worksList", worksUtil);
			    }
		       return model;
		    }
	} catch (Exception e) {
		model.put("status", "9999");
		model.put("errorMessage", "系统异常!");
		throw new MyException("首页查询作品功能异常!"+":"+e);
	}

   }
   
   //点击作品查询作品和留言数据
   @RequestMapping(value="/worksAndExt.do",method=RequestMethod.POST)
   @ResponseBody
   public Map<String,Object> queryWorksAndExt(HttpServletRequest request,HttpServletResponse response) throws MyException{
	   Map<String,Object> model=new HashMap<String, Object>();
	try {	
		   String wid = request.getParameter("wid");
		   Map<String,Object> param=new HashMap<String, Object>();
		   param.put("wid", wid);
		   WorksDto works=null;
		   List<WorksExtDto> worksExts=null;
		   worksAndArticleService.addBrowseNum(wid);
		   Integer worksExtsTote= worksAndArticleService.queryWorksExtNum(wid);
		   works = worksAndArticleService.queryWorks(wid);
		   String  str=works.getWorksImg();
		   String str1=str.substring(0, str.indexOf("/"));
		   String str2=str.substring(str1.length()+1, str.length());
		   works.setWorksImg(str2);
		   worksExts= worksAndArticleService.queryWorksExtList(param);	
		   Collections.sort(worksExts);
		   model.put("worksExtsTote", worksExtsTote);
		   model.put("works", works);
		   model.put("worksExts", worksExts);
		   model.put("status", "0000");			
	} catch (Exception e) {
		model.put("status", "9999");
		model.put("errorMessage", "系统异常!");
		throw new MyException("作品查询作品和留言数据功能异常!"+":"+e);
	}
	   return model;
   }
   
   
   //作品关注
   @RequestMapping(value="/focus.do",method=RequestMethod.POST)
   @ResponseBody
   public Map<String,Object> focusWorks(HttpServletRequest request,HttpServletResponse response) throws MyException{
	   Map<String,Object> model=new HashMap<String, Object>();
	   try {
		   String uid = request.getParameter("uid");
		   String sid = request.getParameter("sid");
		   worksAndArticleService.focusWorks(uid,sid);
		   model.put("status", "0000");  		
	    } catch (Exception e) {
	    	model.put("status", "9999");
			model.put("errorMessage", "系统异常!");
			throw new MyException("作品关注功能异常!"+":"+e);
	   }
	   return model;
   }
   
   //取消关注
   @RequestMapping(value="/cancelFocus.do",method=RequestMethod.POST)
   @ResponseBody
   public Map<String,Object> cancelFocusWorks(HttpServletRequest request,HttpServletResponse response) throws MyException{
	   Map<String,Object> model=new HashMap<String, Object>();
	   try {
		   String uid = request.getParameter("uid");
		   String sid = request.getParameter("sid");
		   UserInfoDto userInfo = updateUserInfoService.queryUserInfo(uid);
		   String  newFocus="";        
           if (userInfo.getFocus().equalsIgnoreCase(sid)) {
        	   newFocus = null;
		   }else if(userInfo.getFocus().endsWith(sid)){
			   newFocus = userInfo.getFocus().replaceAll(","+sid, "");
		   }else{
			   newFocus = userInfo.getFocus().replaceAll(sid+",", ""); 
		   }
		   updateUserInfoService.cancelFocus(uid,newFocus);
		   model.put("status", "0000");  		
	    } catch (Exception e) {
	    	model.put("status", "9999");
			model.put("errorMessage", "系统异常!");
			throw new MyException("作品取消关注功能异常!"+":"+e);
	   }
	   return model;
   }
   
   public List<WorksDto> worksUtil(List<WorksDto> worksList){
	   List<WorksDto> works=new ArrayList<WorksDto>();
		  for (WorksDto work :worksList) {
			  String worksImg = work.getWorksImg();
			  String[] split = worksImg.split(",");
			   work.setWorksImg(split[0]);
			   works.add(work);
		    }
		 return works;
   }
   
}
